<script setup lang="ts">
import { ref, watch } from 'vue'
import * as Icons from '@element-plus/icons-vue'
import { toLine } from '../../utils'
import { useCopy } from '../../hooks/index'

const props = defineProps<{
  title: string,
  visible: boolean
}>()

const dialogVisible = ref(props.visible)

const emits = defineEmits(['update:visible'])

const handleClick = () => {
  emits('update:visible', !props.visible)
}

watch(() => props.visible, val => {
  dialogVisible.value = val
})

watch(dialogVisible, val => {
  emits('update:visible', val)
})

const clickItem = (item: string) => {
  useCopy(`<el-icon-${toLine(item)} />`)
  dialogVisible.value = false
}

</script>

<template>
  <el-button @click="handleClick" type="primary">
    <slot></slot>
  </el-button>
  <el-dialog v-model="dialogVisible" :title="title">
    <div class="container">
      <div v-for="(item, index) in Object.keys(Icons)" :key="index" class="icon-show" @click="clickItem(item)">
          <component :is="`el-icon-${toLine(item)}`"></component>
          {{ item }}
      </div>
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;

  .icon-show {
    flex: 25%;
    display: flex;
    flex-direction: column;
    row-gap: 8px;
    align-items: center;
    margin-bottom: 24px;
    cursor: pointer;

    &:hover {
      color: #409eff;
    }

    svg {
      width: 2em;
      height: 2em;
    }
  }
}
</style>
